#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>

using namespace std;

int dx[5] = { 0 , 0 , 1 , -1 };
int dy[5] = { 1 , -1 , 0 , 0 };
bool visit[10][10];

bool dfs(vector<vector<char>>& board, string word, int row, int col, int pos)
{
    if (pos == word.size())  return true;

    for (int i = 0; i < 4; ++i)
    {
        int x = row + dx[i], y = col + dy[i];
        if (x >= 0 && x < board.size() && y >= 0 && y < board[0].size()
            && visit[x][y] == false && board[x][y] == word[pos])
        {
            visit[x][y] = true;
            if (dfs(board, word, x, y, pos + 1)) return true;
            visit[x][y] = false;
        }
    }

    return false;
}

bool exist(vector<vector<char>>& board, string word) {
    int m = board.size();
    int n = board[0].size();

    for (int i = 0; i < m; ++i)
    {
        for (int j = 0; j < n; ++j)
        {
            if (board[i][j] == word[0] && visit[i][j] == false)
            {
                visit[i][j] = true;
                if (dfs(board, word, i, j, 1))   return true;
                visit[i][j] = false;
            }
        }
    }

    return false;
}


int main()
{


























    int n = 3;
    vector<vector<char>> board;
    board.resize(3);

    board.push_back({'A','B','C','E'});
    board.push_back({'S','F','C','S'});
    board.push_back({'A','D','E','E'});

    exist( board, "ABCCED");
	return 0;
}